<?php
namespace Api\Controller;
use Think\Controller\RestController;
use User\Api\UserApi;

class UserController extends RestController {
	public function _initialize(){
		header("Access-Control-Allow-Origin: http://localhost:3000");
	}
	
	public function register(){
		if(IS_POST){
			$username 	= I('post.username');
			$password 	= I('post.password');
			$repassword = I('post.repassword');
			$mobile 	= I('post.mobile');
			$email 		= $mobile . '@139.com';
			if($password != $repassword){
				$this->response(array('status' => 0, 'msg' => '两次密码不同！'));
			}
	
			$User = new UserApi;
			$uid = $User->register($username, $password, $email, $mobile);
			if(0 < $uid){
				$this->response(array('status' => 1, 'msg' => '注册成功', 'ext' => array('uid' => $uid)));
			} else {
				$this->response(array('status' => 0, 'msg' => $this->_showRegError($uid)));
			}
		}
	}
	
	/**
	 * 获取用户注册错误信息
	 * @param  integer $code 错误编码
	 * @return string        错误信息
	 */
	private function _showRegError($code = 0){
		switch ($code) {
			case -1:  $error = '用户名长度必须在16个字符以内！'; break;
			case -2:  $error = '用户名被禁止注册！'; break;
			case -3:  $error = '用户名被占用！'; break;
			case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
			case -5:  $error = '邮箱格式不正确！'; break;
			case -6:  $error = '邮箱长度必须在1-32个字符之间！'; break;
			case -7:  $error = '邮箱被禁止注册！'; break;
			case -8:  $error = '邮箱被占用！'; break;
			case -9:  $error = '手机格式不正确！'; break;
			case -10: $error = '手机被禁止注册！'; break;
			case -11: $error = '手机号被占用！'; break;
			default:  $error = '未知错误';
		}
		return $error;
	}
	
	public function login($username = '', $password = '', $verify = ''){
		if(IS_POST){
			$username = I('post.username');
			$password = I('post.password');
			$user = new UserApi;
			$uid = $user->login($username, $password);
			if(0 < $uid){
				$Member = D('Member');
				if($Member->login($uid)){
					$this->response(array('status' => 1, 'msg' => '登录成功', 'ext' => array('uid' => $uid, 'password' => $password)));
				} else {
					$this->response(array('status' => 0, 'msg' => $Member->getError()));
				}
			} else { //登录失败
				switch($uid) {
					case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
					case -2: $error = '密码错误！'; break;
					default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
				}
				$this->response(array('status' => 0, 'msg' => $error));
			}

		}
	}
	
	public function changePassword(){
		if (IS_POST) {
			$uid 			= I('post.uid');
			$oldPassword   	=   I('post.oldpassword');
			$newPassword   	=   I('post.newpassword');
			$reNewPassword  =   I('post.renewpassword');
			if (intval($uid) <= 0) {
				$this->response(array('status' => 0, 'msg' => '请先登录'));
			}
			if (empty($oldPassword) || empty($newPassword) || empty($reNewPassword)) {
				$this->response(array('status' => 0, 'msg' => '请完善表单'));
			}
			if($newPassword !== $reNewPassword){
				$this->response(array('status' => 0, 'msg' => '您输入的新密码与确认密码不一致'));
			}
			if (strlen($newPassword) > 30 || strlen($newPassword) < 6) {
				$this->response(array('status' => 0, 'msg' => '密码长度为6-30位'));
			}
		
			$data = array('password' => $newPassword);
			$Api    =   new UserApi();
			$res    =   $Api->updateInfo($uid, $oldPassword, $data);
			if($res['status']){
				$this->response(array('status' => 1, 'msg' => '修改密码成功', 'ext' => array('newpassword' => $newPassword)));
			}else{
				$this->response(array('status' => 0, 'msg' => $res['info']));
			}
		}
	}
	
	public function doProfile(){
		if (IS_POST) {
			//;
		}
	}
	
	public function getProfile(){
		//
	}
	
	public function bindMobile(){
		//
		/**
		 * 初次绑定
		 * 已经绑定后再绑定，就是绑定新手机号
		 */
	}
	
	
}